#pragma once

#include <cstdint>
#include <memory>
#include <map>

#include "net_common.h"
#include "minesweeper.h"
#include "user_manager.h"

/* 全局唯一，可以考虑做成单例 */
class game_manager {
public:
    game_manager(const game_manager &) = delete;
    game_manager &operator=(const game_manager &) = delete;

    static game_manager &get_instance()
    {
        static game_manager instance_;
        return instance_;
    }

    int start_game(uint32_t uid1, uint32_t uid2, std::vector<pos> &bombs_pos, std::vector<pos> &pos_opened_by_system)
    {
        int ret = NET_EC_OK;

        user_manager &user_manager_ptr = user_manager::get_instance();

        /* 获取user */
        auto user1 = user_manager_ptr.get_user(uid1);
        auto player1 = std::make_shared<player>(user1);

        auto user2 = user_manager_ptr.get_user(uid2);
        auto player2 = std::make_shared<player>(user2);

        auto game = std::make_shared<minesweeper>(player1, player2);
        game->init_game(bombs_pos, pos_opened_by_system);

        games_[uid1] = game;
        games_[uid2] = game;

        

        return ret;
    }

    std::shared_ptr<minesweeper> get_game(uint32_t uid)
    {
        return games_[uid];
    }

private:
    game_manager() = default;
    ~game_manager() = default;
    std::map<uint32_t, std::shared_ptr<minesweeper>> games_;    /* uid --> game */
};
